﻿
Namespace effority.UserDirectory

   Partial Class vCard
      Inherits System.Web.UI.Page

      ' View Berechtiging prüfen

      ' vCard Settings prüfen

      'BEGIN:VCARD
      'VERSION:3.0
      'N:Mustermann;Hans
      'FN:Hans Mustermann
      'ORG:Wikipedia
      'URL:http://de.wikipedia.org/
      'EMAIL;TYPE=INTERNET:hans.mustermann@example.org
      'END:VCARD

      Private UserId As Integer

      Private _settings As Hashtable

      Private ModuleId As Int32
      Private TabModuleId As Int32

      Public ReadOnly Property Settings() As Hashtable
         Get

            If _settings Is Nothing Then

               _settings = DotNetNuke.Entities.Portals.PortalSettings.GetTabModuleSettings( _
                               New Hashtable(DotNetNuke.Entities.Portals.PortalSettings.GetModuleSettings(ModuleId)), _
                               New Hashtable(DotNetNuke.Entities.Portals.PortalSettings.GetTabModuleSettings(TabModuleId)))
            End If

            Return _settings

         End Get

      End Property

      Public ReadOnly Property vCardTemplate() As String
         Get

            Dim result As String = String.Empty

            If Not Settings(Consts.VCARD_LINK_TEMPLATE) Is Nothing Then

               result = CStr(Settings(Consts.VCARD_LINK_TEMPLATE))

            End If

            Return result

         End Get

      End Property

      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

         ' Berechtigung prüfen

         If Not Request.Params("UserId") Is Nothing Then

            UserId = CInt(Request.Params("UserId"))

         End If

         If Not Request.Params("ModuleId") Is Nothing Then

            ModuleId = CInt(Request.Params("ModuleId"))

         End If

         If Not Request.Params("TabModuleId") Is Nothing Then

            TabModuleId = CInt(Request.Params("TabModuleId"))

         End If

            Dim info As UserInfo = DotNetNuke.Entities.Users.UserController.GetUser(0, UserId, True)

         Me.Response.Clear()

         Me.Response.AppendHeader("Content-Disposition", "attachment;filename=""vCard.vcf""")

         Me.Response.ContentType = "text/x-vcard"
         Me.Response.ContentEncoding = System.Text.Encoding.UTF8

         Dim UTF8 As New System.Text.UTF8Encoding

         Dim bytes As Byte() = UTF8.GetBytes(GetVCard(info))

         Dim stream As IO.Stream = Me.Response.OutputStream()

         stream.Write(bytes, 0, bytes.Length)

         stream.Flush()
         stream.Close()

         Response.End()

      End Sub

      Private Function GetVCard(ByVal user As UserInfo) As String

         Dim sb As New StringBuilder

         Dim info As New effority.UserDirectory.VCardInfo(user)

         Dim template As String = Me.vCardTemplate

         sb.Append(info.Replace(template))

         Return sb.ToString()

      End Function

   End Class

End Namespace
